package com.zxc.user.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.thread.AppThreadLocalUtil;
import com.zxc.user.mapper.ApUserMapper;
import com.zxc.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.heima.common.constants.UserConstants.USER_FOLLOWERS_LIST;
import static com.heima.common.constants.UserConstants.USER_FOLLOWING_LIST;

@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    @Resource
    private ApUserService apUserService;
    @Resource
    private CacheService cacheService;

    @Override
    public ResponseResult login(LoginDto dto) {
        if (StringUtils.isBlank(dto.getPhone()) && StringUtils.isBlank(dto.getPassword())) {
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }
        System.out.println(dto);

        ApUser dbUser = apUserService.getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, dto.getPhone()));

        if (dbUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户信息不存在");
        }
        String password = dto.getPassword();
        String salt = dbUser.getSalt();
        System.out.println("password:" + password);
        System.out.println("salt:" + salt);
        String asHex = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        System.out.println("asHex:" + asHex);
        if (!asHex.equals(dbUser.getPassword())) {
            System.err.println("==================");
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        String token = AppJwtUtil.getToken(dbUser.getId().longValue());
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        dbUser.setPassword("");
        dbUser.setSalt("");
        map.put("user", dbUser);
        System.out.println(map);
        return ResponseResult.okResult(map);

    }

    @Override
    public ResponseResult userFollow(UserRelationDto dto) {

        if (dto.getAuthorId() == null || dto.getArticleId() == null || dto.getOperation() > 1 || dto.getOperation() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();

        if (user == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        if (dto.getOperation() == 0) {
            cacheService.zAdd(USER_FOLLOWING_LIST + user.getId(), dto.getAuthorId().toString(), System.currentTimeMillis());
            cacheService.zAdd(USER_FOLLOWERS_LIST + dto.getAuthorId(), user.getId().toString(), System.currentTimeMillis());
        } else {
            cacheService.zRemove(USER_FOLLOWING_LIST + user.getId(), dto.getAuthorId().toString(), String.valueOf(System.currentTimeMillis()));
            cacheService.zRemove(USER_FOLLOWERS_LIST + dto.getAuthorId(), user.getId().toString(), String.valueOf(System.currentTimeMillis()));
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
